class ParamError extends Error {
  constructor(msg) {
    super(msg);
    this.name = "ParamError";
  }
}
class HttpError extends Error {
  constructor(msg) {
    super(msg);
    this.name = "HttpError";
  }
}
function ajax(url, success, error) {
  return new Promise((resolve, reject) => {
    // loading.style.display = "block";
    if (!/^http/.test(url)) {
      throw new ParamError("请求地址格式错误");
    }
    let xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.send();
    xhr.onload = function() {
      if (this.status == 200) {
        resolve(JSON.parse(this.response));
        // 回调获取异步的结果
        success && success(JSON.parse(this.response))
      } else if (this.status == 404) {
        // throw new HttpError("用户不存在");
        reject(new HttpError("用户不存在"));
        error && error(new HttpError("用户不存在"))
      } else {
        reject("加载失败");
        error && error('加载失败')

      }
    };
    xhr.onerror = function() {
      reject(this);
      error && error(this)

    };
  });
}
